package com.njupt.sellsec.controller;

import com.njupt.sellsec.result.CodeMsg;
import com.njupt.sellsec.result.Result;
import com.njupt.sellsec.service.SellUserService;
import com.njupt.sellsec.util.ValidatorUtil;
import com.njupt.sellsec.vo.UserFormVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 10:48 2018/7/28
 * @Modified By:
 **/
@Controller
@Slf4j
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private SellUserService sellUserService;

    @GetMapping("/to_login")
    public String toLogin(){
        return "login";
    }

    @PostMapping("/do_login")
    @ResponseBody
    public Result<Boolean> dologin(HttpServletResponse response,
                                   UserFormVo userFormVo){
        log.info("【用户登陆】,uerFormVO={}",userFormVo.toString());
        //表单数据验证
        if (StringUtils.isEmpty(userFormVo.getMobile())){
            return Result.error(CodeMsg.MOBILE_EMPTY);

        }
        if (StringUtils.isEmpty(userFormVo.getPassword())){
            return Result.error(CodeMsg.PASSWORD_EMPTY);
        }
        if (!ValidatorUtil.isMobile(userFormVo.getMobile())){
            return Result.error(CodeMsg.MOBILE_ERROR);
        }
        CodeMsg result = sellUserService.login(response,userFormVo);
        if (result.getCode()==0){
            return Result.success(true);
        }

        return Result.error(result);
    }



}
